package com.miplus.ccrm.report.service.cases;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.report.controller.vo.cases.RepaymentDetailsPageReqVo;
import com.miplus.ccrm.report.controller.vo.cases.RepaymentDetailsPageResVo;
import com.miplus.ccrm.report.pojo.FiCasesRepaymentDetails;
import com.miplus.ccrm.report.mapper.FiCasesRepaymentDetailsMapper;
import com.miplus.ccrm.report.service.ExportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;

/**
* @author Yaohui
* @description 针对表【fi_cases_repayment_details(委案明细表)】的数据库操作Service实现
* @createDate 2025-02-24 17:36:10
*/
@Service
public class FiCasesRepaymentDetailsServiceImpl extends MPJBaseServiceImpl<FiCasesRepaymentDetailsMapper, FiCasesRepaymentDetails>
    implements FiCasesRepaymentDetailsService{

    @Resource
    private ExportService exportService;
    @Override
    public IPage<RepaymentDetailsPageResVo> page(RepaymentDetailsPageReqVo reqVo) {

        return selectJoinListPage(new Page<>(reqVo.getPageNum(),reqVo.getPageSize()), RepaymentDetailsPageResVo.class,
                new MPJLambdaWrapper<>(FiCasesRepaymentDetails.class)
                        .selectAs("fun_getnames('COLLECT.STATUS',t.status)", RepaymentDetailsPageResVo::getStatusDsc)
                        .selectAs("fun_getnames('SORTING.QUEUE',t.disttag)", RepaymentDetailsPageResVo::getDisttag)
                        .selectAs("(select g.grpname from sys_grp g where g.grpid=t.sendtodept)", RepaymentDetailsPageResVo::getSendtodeptdsc)
                        .selectAs("(select name from sys_user where usrid = t.sendtousr)",RepaymentDetailsPageResVo::getSendtousr)
                        .selectAll(FiCasesRepaymentDetails.class)
                        .eq(JPubUtils.IsNotEmpty(reqVo.getCreditcaseid()),FiCasesRepaymentDetails::getCreditcaseid,reqVo.getCreditcaseid())
                        .in(reqVo.getSendtodept()!=null,FiCasesRepaymentDetails::getSendtodept,reqVo.getSendtodept())
                        .in(reqVo.getSendtousr()!=null,FiCasesRepaymentDetails::getSendtousr,reqVo.getSendtousr())
                        .in(reqVo.getDisttag()!=null,FiCasesRepaymentDetails::getDisttag,reqVo.getDisttag())
                        .orderByDesc(FiCasesRepaymentDetails::getCreationTime)
        );
    }

    @Override
    public CmdResVo expRepaymentDetails(String usrid,RepaymentDetailsPageReqVo paras, HttpServletResponse response) {

        IPage<RepaymentDetailsPageResVo> pages = page(paras);

        ArrayList arr=new ArrayList();

        String exptempid=JPubUtils.getExpTempID(paras.getExpfilename(),"回收表现统计表");
        String filename=JPubUtils.getExpFileName(
                paras.getExpfilename(),exptempid
        );
        pages.convert(u->{
            HashMap map=JPubUtils.toExportMap(u);
            if(map!=null) arr.add(map);
            return u;
        });
        CmdResVo resVo=exportService.ToExcel(
                response,
                arr,
                paras.getExpidlist(),
                paras.getExpnamelist(),
                filename,
                usrid
        );

        return resVo;
    }
}




